<--- %%NOBANNER%% --> sortvnames.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Sort the input data sets according to the variable names and save  |
| the data to the output data sets correspondingly;                  |
| Note: the outdata cannot be the same as indata, if so, the variable|
|       names can't be sorted;                                       |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| indata: input dataset names whose variable names need to be sorted;|
| outdata: output dataset names you want the sorted data to be saved;|
|-------------<-- End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: %sortvnames(indata=acedcrf.implant acedcrf.fwp1,          |
|                       outdata=implant fwp1);                       |
| Usage:   %sortvnames(indata, outdata);                             |
\-------------------<-- End of Files Created-->---------------------*/
%macro sortvnames/parmbuff;
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  9-6-2001 11:01pm;                 |
| Modified: 12-24-2001 10:35pm;               |
| Purpose:  Sort variable names of datasets;  |
\--------------------------------------------*/
%local alphaord oriord ndsn indata outdata nin nout _ilme_;
%let inbuff=%qscan(&syspbuff,1,%str((),));
%let outbuff=%qscan(&syspbuff,2,%str((),));
%let linesize = %SYSFUNC(GETOPTION(linesize));
%if (%index(%quote(&syspbuff),%quote(=))) %then %do;
   %if (%index(%quote(&inbuff),%quote(=))) %then %do;
      %if (%index(%quote(%upcase(%sysfunc(compress(%quote(&inbuff))))),%quote(INDATA=))) %then %do;
         %let indata=%qscan(&inbuff,2,%str(=));
         %if (%index(%quote(&outbuff),%quote(=))) %then %do;
            %if (not %index(%quote(%upcase(%sysfunc(compress(%quote(&outbuff))))),%quote(OUTDATA=))) %then %do;
               %put ==> Alert! Keyword parameter "%qscan(&outbuff,1,%str(=))" is not defined!;
            %end;
            %else %do;
               %let outdata=%qscan(&outbuff,2,%str(=));
            %end;
         %end;
         %else %do;
            %let outdata=&outbuff;
         %end;
      %end;
      %else %if (%index(%quote(%upcase(%sysfunc(compress(%quote(&inbuff))))),%quote(OUTDATA=))) %then %do;
         %let outdata=%qscan(&inbuff,2,%str(=));
         %if (%index(%quote(&outbuff),%quote(=))) %then %do;
            %if (not %index(%quote(%upcase(%sysfunc(compress(%quote(&outbuff))))),%quote(INDATA=))) %then %do;
               %put ==> Alert! Keyword parameter "%qscan(&outbuff,1,%str(=))" is not defined!;
            %end;
            %else %do;
               %let indata=%qscan(&outbuff,2,%str(=));
            %end;
         %end;
         %else %do;
            %let indata=&outbuff;
         %end;
      %end;
      %else %put ==> Alert! Keyword parameter "%qscan(&inbuff,1,%str(=))" is not defined!;
   %end;
   %else %if (%index(%quote(&outbuff),%quote(=))) %then %do;
      %if (%index(%quote(%upcase(%sysfunc(compress(%quote(&outbuff))))),%quote(INDATA=))) %then %do;
         %let indata=%qscan(&outbuff,2,%str(=));
         %let outdata=&inbuff;
      %end;
      %else %if (%index(%quote(%upcase(%sysfunc(compress(%quote(&outbuff))))),%quote(OUTDATA=))) %then %do;
         %let indata=&inbuff;
         %let outdata=%qscan(&outbuff,2,%str(=));
      %end;
      %else %do;
         %put ==> Alert! Keyword parameter "%qscan(&outbuff,1,%str(=))" is not defined!;
      %end;
   %end;
%end;
%else %do;
   %let indata=&inbuff;
   %let outdata=&outbuff;
%end;
%if (%quote(&indata) ne) %then %do;
   %if (%quote(&outdata) eq) %then %do;
      %let outdata=&indata;
   %end;
   %let nin=%words(&indata);
   %let nout=%words(&outdata);
   %let linesize = %SYSFUNC(GETOPTION(linesize));
   %if &nin<&nout %then %do;
      %put --> Note: The number of your input data (&nin) is less than the number of;
      %put -->       your output data (&nout);
   %end;
   %do _ilme_=1 %to &nin;
      %let indat&_ilme_=%qscan(&indata,1,%str( ));
      %let outdat&_ilme_=%qscan(&outdata,1,%str( ));
      %if &&outdat&_ilme_ eq %then %do; %let outdat&_ilme_=&&indat&_ilme_; %end;
      %if (%sysfunc(exist(&&indat&_ilme_))) %then %do;
         proc contents noprint data=&&indat&_ilme_ out=_tmpcnt&_ilme_;
         proc sql noprint;
            select name into :alphaord separated by ' '
            from _tmpcnt&_ilme_ order by upcase(name);

            select name into :oriord separated by ' '
            from _tmpcnt&_ilme_
            order by varnum;
         quit;
         %put --> All variable names listed in their original order:;
         %put &oriord..;
         %put %_repeat('-',&linesize);
         %put --> All variable names listed in Alphabatic orders:;
         %put &alphaord..;
         %put %_repeat('-',&linesize);
         data &&outdat&_ilme_;
            retain %upcase(&alphaord);
            set &&indat&_ilme_;
         run;
         proc datasets library=work nolist;
              delete _tmpcnt&_ilme_;
         run;quit;
      %end;
      %else %do;
         %put ==> Alert! Please check your input, the data set "&dsn";
         %put ==>        doesn%str(%')t exist!;
      %end;
   %end;
%end;
%else %do;
   %put ==> Alert! No data is to be sorted!;
%end;
%mend sortvnames;